package com.catbad.services.impl;

import com.catbad.domain.Play;
import com.catbad.domain.Room;
import com.catbad.mapper.PlayMapper;
import com.catbad.mapper.RoomMapper;
import com.catbad.services.RoomServices;
import com.catbad.vo.RoomVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class RoomServicesImpl implements RoomServices {
    @Autowired
    RoomMapper roomMapper;
    @Override
    public List<Map<String, Object>> getRoomListForPage(Long cid, Long filmId) {
        Map<String,Object> params = new HashMap<>();
        params.put("cid",cid);
        params.put("filmId",filmId);
        //获得全部改影院全部影厅
        List<Map<String,Object>> roomList = roomMapper.getRoomListForPage(params);
        return roomList;
    }

    @Override
    public Room getRoomById(Long roomId) {
        return roomMapper.selectByPrimaryKey(roomId);
    }

    @Override
    public RoomVO getRoomListForRoomVO(Integer pageNo, Long cid) {
        //获得对应影院，影厅信息
        RoomVO roomVO = new RoomVO();
        //参数
        Map<String,Object> params = new HashMap<>();
        params.put("cid",cid);
        //处理分页数据
        int rows = 10;
        if(pageNo==null){
            pageNo=1;
        }
        if(pageNo<1){
            pageNo=1;
        }
        Long total = roomMapper.getRoomTotal(params);
        int maxPage= (int)(total%rows==0?total/rows:total/rows+1);
        maxPage = Math.max(1,maxPage);
        if(pageNo>maxPage){
            pageNo=maxPage;
        }
        int start = (pageNo-1)*rows;
        int length = rows;
        params.put("start",start);
        params.put("length",length);
        //查询结果
        List<Room> roomList = roomMapper.getRoomListByLimit(params);
        //页面数据存入
        roomVO.setRoomList(roomList);
        roomVO.setRows(rows);
        roomVO.setMaxPage(maxPage);
        roomVO.setTotal(total);
        roomVO.setPageNo(pageNo);
        return roomVO;
    }

    @Override
    public int saveRoom(String roomName,Long cid) {
        Room room = new Room();
        room.setCid(cid);
        room.setRoomName(roomName);
        room.setRdeleteFlag("1");
        room.setSeatInfo("[[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]]");
        if(roomMapper.selectByName(roomName)!=null){
            return 2;
        }
        return roomMapper.saveRoom(room);
    }

    @Override
    public int deleteRoomById(Long roomId) {
        return roomMapper.deleteRoomById(roomId);
    }

    @Override
    public Room selectRoomById(Long roomId) {
        return roomMapper.selectByPrimaryKey(roomId);
    }

    @Override
    public int updateRoomNameById(Long roomId,String roomName) {
        Map<String,Object> params = new HashMap<>();
        params.put("roomId",roomId);
        params.put("roomName",roomName);
        return roomMapper.updateRoomNameById(params);
    }
}
